package com.gui;


import java.util.ArrayList;
import java.util.List;

import org.teleal.cling.android.AndroidUpnpService;
import org.teleal.cling.model.meta.RemoteDevice;

import com.phonegap.DroidGap;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class Browser extends DroidGap {

	private static final String TAG = Browser.class.getName();
	private AndroidUpnpService upnpService;
	List<RemoteDevice> DMSServerList = new ArrayList<RemoteDevice>();
	
	private BrowseRegistryListener registryListener = new BrowseRegistryListener();
		
	private ServiceConnection serviceConnection = new ServiceConnection(){

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			upnpService = (AndroidUpnpService) service;
			registryListener.setService(upnpService);	
			upnpService.getRegistry().addListener(registryListener);
			upnpService.getControlPoint().search();			
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {			
			upnpService = null;
		}		
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);		
		getApplicationContext().bindService(
        		new Intent(this, BrowserUpnpService.class), serviceConnection, Context.BIND_AUTO_CREATE
        );		
		super.loadUrl("file:///android_asset/www/index.html");			
	}
	
	public BrowseRegistryListener getBrowseRegistryListener(){
		DMSServerList = registryListener.getDMSServer();
		Log.e(TAG, "DMSServerList.size() : " + DMSServerList.size());
		
		return registryListener;
	}	
}